﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace ExtSite
{
    using System.Net;
    using Microsoft.Reporting.WebForms;
    using System.Security.Principal;

    public class Credentials : NetworkCredential
    {
        public Credentials()
        { }
        public Credentials(string domain, string username, string password)
        {
            this.UserName = username;
            this.Password = password;
            this.Domain = domain;
            //NetworkCredential cred = new NetworkCredential(username, password, "FSOFT-HCM");
        }
        public Credentials(string username, string password)
        {
            this.UserName = username;
            this.Password = password;
            this.Domain = ClsFunctions.getPara("DOMAINNAME");
            //NetworkCredential cred = new NetworkCredential(username, password, "FSOFT-HCM");
        }

        public NetworkCredential GetCredential(string username, string password)
        {
            NetworkCredential cred = new NetworkCredential(username, password, ClsFunctions.getPara("DOMAINNAME"));
            return cred;
        }
    }

    public class ReportServerCredentials : IReportServerCredentials
    {
        public WindowsIdentity ImpersonationUser
        {
            get
            {
                // Use the default Windows user.  Credentials will be
                // provided by the NetworkCredentials property.
                return null;
            }
        }

        public ICredentials NetworkCredentials
        {
            get
            {
                // Read the user information from the Web.config file.  
                // By reading the information on demand instead of 
                // storing it, the credentials will not be stored in 
                // session, reducing the vulnerable surface area to the
                // Web.config file, which can be secured with an ACL.

                // User name
                string userName =
                    ClsFunctions.getPara("ReportViewerUser");

                if (string.IsNullOrEmpty(userName))
                    throw new Exception(
                        "Missing user name from web.config file");

                // Password
                string password =
                    ClsFunctions.getPara("ReportViewerPassword");

                if (string.IsNullOrEmpty(password))
                    throw new Exception(
                        "Missing password from web.config file");

                // Domain
                string domain = ClsFunctions.getPara("DOMAINNAME");

                if (string.IsNullOrEmpty(domain))
                    throw new Exception(
                        "Missing domain from web.config file");

                return new NetworkCredential(userName, password, domain);
            }
        }

        public bool GetFormsCredentials(out Cookie authCookie,
                    out string userName, out string password,
                    out string authority)
        {
            authCookie = null;
            userName = null;
            password = null;
            authority = null;

            // Not using form credentials
            return false;
        }
    }
}
